package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.SaAdminCheckPermission;
import com.cskaoyan.wordmemorize.common.constant.ResultCodeEnum;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocChapBookDTO;
import com.cskaoyan.wordmemorize.dto.admin.VocDTO;
import com.cskaoyan.wordmemorize.request.BatchDeleteCommand;
import com.cskaoyan.wordmemorize.request.VocCommand;
import com.cskaoyan.wordmemorize.request.VocPageRequest;
import com.cskaoyan.wordmemorize.service.FileUploadService;
import com.cskaoyan.wordmemorize.service.VocService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

// todo【后台管理-物料管理-单词管理相关接口】的部分
@RestController
@RequestMapping("/admin/voc")
public class AdminVocController {


    @Autowired
    VocService vocService;

    @Autowired
    FileUploadService fileUploadService;


    // todo 添加单词
    @SaAdminCheckPermission("admin:voc:add")
    @PostMapping("")
    public Result addVoc(@RequestBody VocCommand vocCommand) {

        vocService.addVoc(vocCommand);

        return Result.ok();
    }

    // todo 修改单词
    @SaAdminCheckPermission("admin:voc:update")
    @PutMapping("")
    public Result updateVoc(@RequestBody VocCommand vocCommand) {

        vocService.updateVoc(vocCommand);

        return Result.ok();
    }

    // todo 删除单词与书籍和章节的关系
    @DeleteMapping("/relation/{id}")
    public Result deleteVocRelation(@PathVariable Long id) {

        vocService.deleteVocRelation(id);

        return Result.ok();
    }

    // todo 删除单词释义
    @DeleteMapping("interpretation/{id}")
    public Result deleteInterpretation(@PathVariable Long id) {

        vocService.deleteInterpretation(id);
        return Result.ok();
    }
    // todo 删除单词例句
    @DeleteMapping("example/{id}")
    public Result deleteExample(@PathVariable Long id) {

        vocService.deleteExample(id);
        return Result.ok();
    }

    // todo 指定单词的单词-章节-书籍关系列表
    @GetMapping("relations/{vocId}")
    public Result<List<VocChapBookDTO>> getVocRelations(@PathVariable Long vocId) {

        List<VocChapBookDTO> vocRelations = vocService.getVocRelations(vocId);
        return Result.ok(vocRelations);
    }

    // todo 上传单词对应的音频文件
    @PostMapping("/file")
    public Result uploadVocFile(@RequestParam("file") MultipartFile file) {
        try{
            // todo 判断上传文件是否存在
            if(file.isEmpty()){
                return Result.fail(ResultCodeEnum.FILE_NOT_EXIST);
            }
            // todo 判断是否为音频文件
            String originalFilename = file.getOriginalFilename();
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            if (!Arrays.asList(".mp3", ".wma", ".wav",".ape", ".flac", ".ogg", ".aac").contains(extension.toLowerCase())) {
                return Result.fail(ResultCodeEnum.NOT_IS_AUDIO_FILE);
            }

            // todo 上传文件到OSS
            String upload = fileUploadService.upload(file);
            return Result.ok(upload);
        } catch (IOException e) {
            return Result.fail(ResultCodeEnum.FAIL);
        }
    }

    // todo 删除单词对应音频文件
    @DeleteMapping("/file/{type}/{id}")
    public Result deleteFile(@PathVariable String type, @PathVariable Long id, @RequestParam String name){
        vocService.deleteFile(id, type, name);
        return Result.ok();
    }
    // todo 分页查询单词列表
    @GetMapping("list")
    public Result<PageDTO<VocDTO>> getVocList(VocPageRequest pageQuery){
        PageDTO<VocDTO> page = vocService.getVocabularies(pageQuery);
        return Result.ok(page);
    }

    // todo:删除指定单词
    @SaAdminCheckPermission("admin:voc:delete")
    @DeleteMapping({"{vocId}"})
    public Result deleteVocabulary(@PathVariable("vocId") Long vocId) {
        vocService.deleteVocabulary(vocId);
        return Result.ok();
    }

    // todo:批量删除单词
    @SaAdminCheckPermission("admin:voc:batchdelete")
    @DeleteMapping("")
    public Result deleteVocabularies(@RequestBody BatchDeleteCommand batchDeleteCommand) {
        vocService.deleteVocabularies(batchDeleteCommand);
        return Result.ok();
    }
}
